﻿/********
* 
* 
*  Description:
*  Create By kain.hong at 8/9/2010 2:34:19 PM
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/
#region namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;

#endregion


namespace Cherry.Agile.ServiceModel
{
    internal class ReflectionUtility
    {

        // Methods
        private static T GetAttribute<T>(MemberInfo memberInfo) where T : class
        {
            object[] customAttributes = memberInfo.GetCustomAttributes(typeof(T), false);
            if (customAttributes.Length > 0)
            {
                return (T)customAttributes[0];
            }
            return default(T);
        }

        internal static DataContractAttribute GetDataContractAttribute(Type type)
        {
            return GetAttribute<DataContractAttribute>(type);
        }

        internal static Dictionary<string, MemberInfo> GetEnumMembers(Type type)
        {
            bool flag = GetDataContractAttribute(type) != null;
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
            Dictionary<string, MemberInfo> dictionary = new Dictionary<string, MemberInfo>(fields.Length);
            FieldInfo[] infoArray2 = fields;
            for (int i = 0; i < infoArray2.Length; i++)
            {
                MemberInfo memberInfo = infoArray2[i];
                if (flag)
                {
                    EnumMemberAttribute attribute = GetAttribute<EnumMemberAttribute>(memberInfo);
                    if (attribute != null)
                    {
                        string name = attribute.Value;
                        if (string.IsNullOrEmpty(name))
                        {
                            name = memberInfo.Name;
                        }
                        dictionary.Add(name, memberInfo);
                    }
                }
                else
                {
                    dictionary.Add(memberInfo.Name, memberInfo);
                }
            }
            return dictionary;
        }

        internal static object GetValue(object obj, string propertyName)
        {
            MemberInfo info = obj.GetType().GetMember(propertyName, MemberTypes.Property | MemberTypes.Field, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)[0];
            if (info.MemberType == MemberTypes.Field)
            {
                return ((FieldInfo)info).GetValue(obj);
            }
            return ((PropertyInfo)info).GetValue(obj, null);
        }

        internal static void SetValue(object obj, string propertyName, object value)
        {
            MemberInfo info = obj.GetType().GetMember(propertyName, MemberTypes.Property | MemberTypes.Field, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)[0];
            if (info.MemberType == MemberTypes.Field)
            {
                ((FieldInfo)info).SetValue(obj, value);
            }
            else
            {
                ((PropertyInfo)info).SetValue(obj, value, null);
            }
        }
    }

}
